﻿using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;

namespace Chengqbd.Core.Extensions
{
	public static class RedisExtensions
	{
		public static bool IsExist(this IDistributedCache cache, string key)
		{
			bool flag = cache.Get(key) == null;
			return !flag;
		}
        
		public static bool TryGetValue<TItem>(this IDistributedCache cache, string key, out TItem value)
		{
			byte[] array = cache.Get(key);
			bool flag = array == null;
			bool result;
			if (flag)
			{
				value = default(TItem);
				result = false;
			}
			else
			{
				string @string = Encoding.UTF8.GetString(array);
				value = JsonConvert.DeserializeObject<TItem>(@string);
				result = true;
			}
			return result;
		}
        
		public static async Task<bool> SetLockAsync<TItem>(this IDistributedCache cache, string key, TItem value, CancellationToken token = default(CancellationToken))
		{
			return await cache.SetLockAsync(key, value, new DistributedCacheEntryOptions(), token);
		}
        
		public static async Task<bool> SetLockAsync<TItem>(this IDistributedCache cache, string key, TItem value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
		{
			bool isExist = cache.IsExist(key);
			bool flag = isExist;
			bool result;
			if (flag)
			{
				result = true;
			}
			else
			{
				await cache.SetAsync(key, value, options, token);
				result = false;
			}
			return result;
		}
        
		public static async Task<bool> SetLockAsync(this IDistributedCache cache, string key, string value, CancellationToken token = default(CancellationToken))
		{
			return await cache.SetLockAsync(key, value, new DistributedCacheEntryOptions(), token);
		}
        
		public static async Task<bool> SetLockAsync(this IDistributedCache cache, string key, string value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
		{
			bool isExist = cache.IsExist(key);
			bool flag = isExist;
			bool result;
			if (flag)
			{
				result = true;
			}
			else
			{
				await DistributedCacheExtensions.SetStringAsync(cache, key, value, options, token);
				result = false;
			}
			return result;
		}
        
		public static Task SetAsync<TItem>(this IDistributedCache cache, string key, TItem value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
		{
			bool flag = key == null;
			if (flag)
			{
				throw new ArgumentNullException("key");
			}
			bool flag2 = value == null;
			if (flag2)
			{
				throw new ArgumentNullException("value");
			}
			string s = JsonConvert.SerializeObject(value);
			byte[] bytes = Encoding.UTF8.GetBytes(s);
			return cache.SetAsync(key, bytes, options, token);
		}
        
		public static Task SetAsync<TItem>(this IDistributedCache cache, string key, TItem value, CancellationToken token = default(CancellationToken))
		{
			return cache.SetAsync(key, value, new DistributedCacheEntryOptions(), token);
		}
		public static async Task<TItem> GetAsync<TItem>(this IDistributedCache cache, string key, CancellationToken token = default(CancellationToken))
		{
			byte[] array = await cache.GetAsync(key, token);
			byte[] model = array;
			array = null;
			string json = Encoding.UTF8.GetString(model);
			return JsonConvert.DeserializeObject<TItem>(json);
		}
        
		public static async Task<TItem> GetOrCreateAsync<TItem>(this IDistributedCache cache, string key, TItem value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
		{
			byte[] array = await cache.GetAsync(key, token);
			byte[] model = array;
			array = null;
			TItem result;
			if (model == null)
			{
				await cache.SetAsync(key, value, options, token);
				result = value;
			}
			else
			{
				string json = Encoding.UTF8.GetString(model);
				result = JsonConvert.DeserializeObject<TItem>(json);
			}
			return result;
		}
        
		public static async Task<TItem> GetOrCreateAsync<TItem>(this IDistributedCache cache, string key, TItem value, CancellationToken token = default(CancellationToken))
		{
			return await cache.GetOrCreateAsync(key, value, new DistributedCacheEntryOptions(), token);
		}
	}
}
